/**
 * 
 */
package ar.edu.unq.svnpermission.ui.pages;



import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import ar.edu.unq.svnpermission.domain.Group;
import ar.edu.unq.svnpermission.domain.User;
import ar.edu.unq.svnpermission.panels.LinkPanel;
import ar.edu.unq.svnpermission.ui.model.Groups;

/**
 * @author nicolas
 */
public class NewGroupPage extends InternalPage<Groups> {
	private static final long serialVersionUID = 1L;
	private AjaxFallbackDefaultDataTable<User> availableUsersTable;
	private AjaxFallbackDefaultDataTable<User> addedUsersTable;
	
	
	public NewGroupPage()
	{
		
	}
	
	public NewGroupPage(Group group)
	{
		super();
		((Groups)this.getForm().getDefaultModelObject()).setGroup(group);
	}	
	
	@Override
	protected void addFields() {
		this.addKeyNameField();
		this.addAvailableUsersTable();
		this.addAddedUsersTable();
	}
	
	private void addKeyNameField()
	{
		this.getForm().add(new TextField<String>("keyName"));
	}
	
	private void addAvailableUsersTable()
	{

        List<IColumn<User>> columns = new ArrayList<IColumn<User>>();
        columns.add(new PropertyColumn<User>(new Model<String>("Usuario"), "toString"));      
        columns.add(new AbstractColumn<User>(new Model<String>("Eliminar")) {           
			private static final long serialVersionUID = 1L;
			public void populateItem(Item<ICellPopulator<User>> item, String id, IModel<User> model) {  
                LinkPanel<User> delete = new LinkPanel<User>(id,model,"Agregar") {
					private static final long serialVersionUID = 1L;

					@Override 
                    public void onClick(AjaxRequestTarget art) {                        
						Groups groupsModel = (Groups)NewGroupPage.this.getForm().getDefaultModelObject();
						groupsModel.addToAddedUsers(this.getModelObject());
						NewGroupPage.this.availableUsersTable.modelChanged();
						NewGroupPage.this.addedUsersTable.modelChanged();
						art.add(NewGroupPage.this.availableUsersTable);
						art.add(NewGroupPage.this.addedUsersTable);
                    } 
                }; 
                item.add(delete);                
            }  
        });        
        this.availableUsersTable = new AjaxFallbackDefaultDataTable<User>("availableUsers", columns, ((Groups)this.getForm().getDefaultModelObject()).getAvailableUsersDataProvider() , 5);
        this.getForm().add(this.availableUsersTable);
	}	
	
	private void addAddedUsersTable()
	{
        List<IColumn<User>> columns = new ArrayList<IColumn<User>>();
        columns.add(new PropertyColumn<User>(new Model<String>("Usuario"), "toString"));      
        columns.add(new AbstractColumn<User>(new Model<String>("Eliminar")) {           
			private static final long serialVersionUID = 1L;
			public void populateItem(Item<ICellPopulator<User>> item, String id, IModel<User> model) {  
                LinkPanel<User> delete = new LinkPanel<User>(id,model,"Eliminar") {
					private static final long serialVersionUID = 1L;

					@Override 
                    public void onClick(AjaxRequestTarget art) {                        
						Groups groupsModel = (Groups)NewGroupPage.this.getForm().getDefaultModelObject();
						groupsModel.addToAvailableUsers(this.getModelObject());
						NewGroupPage.this.availableUsersTable.modelChanged();
						NewGroupPage.this.addedUsersTable.modelChanged();
						art.add(NewGroupPage.this.availableUsersTable);
						art.add(NewGroupPage.this.addedUsersTable);						
                    } 
                }; 
                item.add(delete);                
            }  
        });        
        this.addedUsersTable = new AjaxFallbackDefaultDataTable<User>("addedUsers", columns, ((Groups)this.getForm().getDefaultModelObject()).getAddedUsersDataProvider() , 5);
        this.getForm().add(this.addedUsersTable);		
	}
	


	@Override
	protected void addButtons() {
		this.getForm().add(new Button("createGroup") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
					((Groups) this.getParent().getDefaultModelObject()).create();
					this.setResponsePage(new GroupsPage());
			}
		});
		
	}

	@Override
	protected CompoundPropertyModel<Groups> createModel() {		
		return new CompoundPropertyModel<Groups>(new Groups());
	}

	@Override
	protected String getFormName() {
		return "newGroupForm";
	}

}
